package com.sc.syllabus.money;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
import com.sc.syllabus.R;
import com.sc.syllabus.utils.ToastUtil;
import com.srain.utils.StringUtils;

import java.util.Date;

/**
 * Created by liushuanglong on 2017/9/13.
 */

public class MoneyEditActivity extends FragmentActivity {

    private static final String TAG = "MoneyEditActivity";
    private static final String EXTRA_BEAN = "bean";
    private TextView txtStart, txtEnd;
    private EditText editTitle, editAmount, editLl, txtContent;
    private RadioGroup rGroup;
    private RadioButton rbtnNato, rbtnHome;
    private Button btnSave;
    private MoneyInfo bean;
    private MoneyModelImpl mModel;
    private boolean hasExtra = false;
    private long startTime;
    private long endTime;

    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, MoneyEditActivity.class);
        activity.startActivity(intent);
    }

    public static void actionStart(Activity activity, MoneyInfo bean) {
        Intent intent = new Intent(activity, MoneyEditActivity.class);
        intent.putExtra(EXTRA_BEAN, bean);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_money_info);
        mModel = new MoneyModelImpl(this);
        init();
    }

    private void init() {
        editTitle = (EditText) findViewById(R.id.text_title);
        editAmount = (EditText) findViewById(R.id.text_amount);
        editLl = (EditText) findViewById(R.id.text_ll);
        txtStart = (TextView) findViewById(R.id.text_startAt);
        txtEnd = (TextView) findViewById(R.id.text_endAt);
        txtContent = (EditText) findViewById(R.id.text_content);
        rGroup = (RadioGroup) findViewById(R.id.rgroup);
        rbtnNato = (RadioButton) findViewById(R.id.rbtn_in);
        rbtnHome = (RadioButton) findViewById(R.id.rbtn_out);
        btnSave = (Button) findViewById(R.id.btn_sub);

        rGroup.setOnCheckedChangeListener(mCheckedListener);
        txtStart.setOnClickListener(mClickListener);
        txtEnd.setOnClickListener(mClickListener);
        btnSave.setOnClickListener(mClickListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hasExtra = this.getIntent().hasExtra(EXTRA_BEAN);
        if (hasExtra) {
            bean = this.getIntent().getParcelableExtra(EXTRA_BEAN);
            editTitle.setText(bean.getTitle());
            editAmount.setText("金额 :" + bean.getAmount());
            editLl.setText("利率 :" + bean.getInterest());
            txtStart.setText(StringUtils.numericToStr(String.valueOf(bean.getDate_jk())));
            txtEnd.setText(StringUtils.numericToStr(String.valueOf(bean.getDate_yqhk())));
            txtContent.setText(bean.getContent());
        } else {
            if (null == bean) {
                bean = new MoneyInfo();
            }
        }
    }

    private RadioGroup.OnCheckedChangeListener mCheckedListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rbtn_in:
                    bean.setInterest(1);
                    break;
                case R.id.rbtn_out:
                    bean.setInterest(0);
                    break;
            }
        }
    };

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_sub:
                    save();
                    break;

                case R.id.text_startAt:
                    showDateDialog(1);
                    break;

                case R.id.text_endAt:
                    showDateDialog(2);
                    break;
            }
        }
    };

    private void save() {
        int id = 0;
        bean.setTitle(editTitle.getText().toString().trim());
        bean.setAmount(Double.valueOf(editAmount.getText().toString().trim()));
        bean.setInterest(Double.valueOf(editLl.getText().toString().trim()));
        bean.setDate_jk(startTime);
        bean.setDate_yqhk(endTime);
        bean.setContent(txtContent.getText().toString().trim());
        if (hasExtra) {
            id = mModel.modify(bean);
        } else {
            id = (int) mModel.save(bean);
        }

        if (0 == id) {
            ToastUtil.show(MoneyEditActivity.this, "保存失败,请重新保存!");
        } else if (id > 0) {
            ToastUtil.show(MoneyEditActivity.this, "保存成功, id == " + id);
            MoneyEditActivity.this.finish();
        } else if (id < 0) {
            ToastUtil.show(MoneyEditActivity.this, "保存失败, id == " + id);
        }
    }

    private void showDateDialog(int flag) {
        new SlideDateTimePicker.Builder(getSupportFragmentManager())
                .setListener(new SlideListener(flag))
                .setInitialDate(new Date())
                .setIs24HourTime(true)
                .build()
                .show();
        //.setMinDate(minDate)
        //.setMaxDate(maxDate)
        //.setIs24HourTime(true)
        //.setTheme(SlideDateTimePicker.HOLO_DARK)
        //.setIndicatorColor(Color.parseColor("#990000"))
    }

    private class SlideListener extends SlideDateTimeListener {

        private int flag = 0;

        public SlideListener(int flag) {
            this.flag = flag;
        }

        @Override
        public void onDateTimeSet(Date date) {

            switch (flag) {
                case 1:  // 开始时间
                    startTime = date.getTime() / 1000;
                    txtStart.setText("开始时间: " + StringUtils.infoFormat.format(date));
                    break;
                case 2:  // 结束时间
                    endTime = date.getTime() / 1000;
                    txtEnd.setText("结束时间: " + StringUtils.infoFormat.format(date));
                    break;
            }
        }

        @Override
        public void onDateTimeCancel() {
            ToastUtil.show(MoneyEditActivity.this, "Canceled");
        }
    }
}

